package com.yang.sqlBuilder;

import org.apache.ibatis.jdbc.SQL;

import com.yang.entity.User;

public class AnnotationSqlBuilder {

	public String getUser(final User user) {
		return new SQL() {
			{
				SELECT("*");
				FROM("user");
				if (user.getName() != null) {
					WHERE("name like '%' || #{name} || '%'");
				}
				ORDER_BY("id");
			}
		}.toString();
	}

	public String deleteUserSql() {
		return new SQL() {
			{
				DELETE_FROM("user");
				WHERE("id = #{id}");
			}
		}.toString();
	}

	public String insertUserSql(final User user) {
		return new SQL() {
			{
				INSERT_INTO("user");
				if(user.getName() != null){
					VALUES("name", "#{name}");
				}
				if(user.getSex() != null && user.getCash() != null){
					VALUES("sex,cash", "#{sex},#{cash}");
				}
			}
		}.toString();
	}

	public String updateUserSql(final User user) {
		return new SQL() {
			{
				UPDATE("user");
				if(user.getName() != null){
					SET("name = #{name}");
				}
				if(user.getSex() != null){
					SET("sex = #{sex}");
				}
				WHERE("id = #{id}");
			}
		}.toString();
	}
}
